Skip to content

Downcast nanosecond timestamps and times before partitioning - #4479

Open
rahulsmahadev wants to merge 4 commits into
apache:mainfrom
rahulsmahadev:rahul/ns-timestamp-hour-partition
Open

rahulsmahadev wants to merge 4 commits into
apache:mainfrom
rahulsmahadev:rahul/ns-timestamp-hour-partition

Conversation

@rahulsmahadev

@rahulsmahadev rahulsmahadev commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Rationale for this change

Bug: with downcast-ns-timestamp-to-us-on-write on (non-default; set here with PYICEBERG_DOWNCAST_NS_TIMESTAMP_TO_US_ON_WRITE=true), an hour partition on a timestamp[ns] column fails. The partition values are computed on the nanosecond input, and the writer only downcasts afterwards. The hour transform has no nanosecond kernel:

# table partitioned by hour(event_ts)
tbl.append(pa.table({"id": [0, 1], "event_ts": pa.array([1000000001500, 3600000000000], pa.timestamp("ns"))}))
# ValueError: FeatureUnsupported => Unsupported data type for hour transform: Timestamp(Nanosecond, None)

For an identity partition on the same column, main writes the aligned row twice and drops the unaligned one (1000 and 1500 ns give IDs [0, 0]), and raises ZeroDivisionError when no aligned row shares an unaligned value's microsecond. For year, month and day partitions, a -1 ns row is stored as 0 us but filed under partition -1, so a filter like ts >= '1970-01-01T00:00:00' skips it.

Since #4471 the option also downcasts time64[ns], and an identity partition on a time column has the same problem: 1000 and 1500 ns give IDs [0, 0], and an unaligned value alone in its microsecond raises ZeroDivisionError.

Fix: when the option is on, cast each nanosecond partition source (timestamp or time) to microseconds with the writer's safe=False cast before applying its transform. The writer still downcasts the data, so partition values match stored values. Refs #1619, the same conversion-order problem for the day transform, closed as stale.

Java's v1/v2 partition transforms receive microseconds: Spark supplies them directly, and the generic Kafka writer uses InternalRecordWrapper to convert timestamps and times first. This PR applies PyIceberg's existing downcast before partitioning.

Are these changes tested?

Yes. Three tests in tests/io/test_pyarrow.py, next to the existing downcast tests. test_append_partitioned_ns_timestamp_downcast appends 1000, 1500, -1, 3600000000000 ns and a null to identity, hour and UTC day partitions, and checks the stored microseconds and the rows per partition. Each case fails on main: identity with ZeroDivisionError, hour with the FeatureUnsupported error above, and day with the -1 ns row in partition -1. test_append_partitioned_ns_time_downcast does the same for an identity partition on a time column (1000, 1500 and 3600000001500 ns and a null); it fails on main with ZeroDivisionError. test_determine_partitions_native_ns_timestamp_is_not_downcast checks that v3 nanosecond sources keep -1 ns; it fails if the native-type check is removed.

Are there any user-facing changes?

With the option on, hour partitions accept timestamp[ns] and identity partitions on timestamp and time columns write every row once. Year, month and day partitions now follow the stored microseconds at negative boundaries (-1 ns becomes 0 us, so partition -1 becomes 0), so filters no longer skip such rows. Existing files retain their old partition values.

Please add the changelog label.

This pull request and its description were written by Isaac.

Partition values were computed on timestamp[ns] input before the writer
downcast it, so identity partitions duplicated or lost rows, hour partitions
failed, and negative year/month/day partitions disagreed with the stored
values. Cast each nanosecond partition source to timestamp[us] before its
transform when downcast-ns-timestamp-to-us-on-write is set.

Co-authored-by: Isaac <no-reply@databricks.com>
@rahulsmahadev

Copy link
Copy Markdown
Contributor Author

@ebyhr, this fixes hour and identity partitions on timestamp[ns] when downcast-ns-timestamp-to-us-on-write is on, by computing partition values on the microseconds that get written. Could you take a look when you have a chance? Thanks!

Cover identity, hour and UTC day partitions in one loop over the same values,
tie each partition to its rows in the native ns test, and move the tests after
the related _to_requested_schema pair.

Co-authored-by: Isaac <no-reply@databricks.com>
@korsakowii

korsakowii commented Nov 26, 2026 •

Copy link
Copy Markdown

I tested seven synthetic cases across Iceberg v1/v2 tables, covering identity, hour, and UTC-day partitions. All passed with this fix, with correct stored rows, partition values, and filtered scans.

I also checked the existing-file caveat mentioned in the PR. I wrote two separate rows with the same input timestamp (1969-12-31 23:59:59.999999999 UTC, nanosecond precision), one before and one after the fix.

Before fix With this fix
Stored timestamp (µs precision, UTC) 1970-01-01 00:00:00 1970-01-01 00:00:00
Day partition 1969-12-31 (-1) 1970-01-01 (0)

A full scan followed by filtering returned both rows, but filtering directly in table.scan() returned only the newly written row. The legacy file was skipped because of its incorrect partition value.

Handling affected legacy files separately makes sense and keeps this PR focused.

rahulsmahadev and others added 2 commits November 29, 2026 02:22
Co-authored-by: Isaac <no-reply@databricks.com>
apache#4471 made the downcast option cover time64[ns] as well, with the same
ordering problem: an identity partition on a time column grouped rows by
nanoseconds, so 1000 and 1500 ns (both 1 us) wrote one row twice and
dropped the other, and an unaligned value alone in its microsecond raised
ZeroDivisionError. Cast ns times to microseconds before the transform, as
for timestamps.

Co-authored-by: Isaac <no-reply@databricks.com>
@rahulsmahadev rahulsmahadev changed the title Downcast nanosecond timestamps before partitioning Downcast nanosecond timestamps and times before partitioning Nov 30, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants